<template>
  <el-dialog title="指定环节" append-to-body :visible.sync="dialogFormVisible" width="500px" @close="close" :close-on-click-modal="false">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="选择环节" prop="targetNodeId">
        <el-select v-model="form.targetNodeId" placeholder="请选择">
          <el-option v-for="item in flowNodes" :key="item.nodeId" :label="item.nodeName" :value="item.nodeId"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doRejectTaskToAny, getTaskFlowData } from '@/api/workflow/task'

  export default {
    name: 'SalesOrderFlowImage',
    data() {
      return {
        dialogFormVisible: false,
        form: {
          targetNodeId: undefined,
        },
        rules: { targetNodeId: [{ required: true, trigger: ['blur', 'change'], message: '请选择环节' }] },
        flowNodes: [],
      }
    },
    created() {},
    mounted() {},
    methods: {
      showEdit(row) {
        this.dialogFormVisible = true
        this.form = Object.assign({}, row)
        this.getTaskFlowData()
      },
      async getTaskFlowData() {
        await getTaskFlowData({ taskId: this.form.id }).then((res) => {
          this.flowNodes = res.data.canBackNodes
        })
      },
      close() {
        this.dialogFormVisible = false
      },
      save() {
        console.log(this.form)
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            this.$baseConfirm('确定要驳回选择的环节吗？', null, async () => {
              let data = {
                category: this.form.category,
                businessKey: this.form.businessKey,
                taskId: this.form.id,
                comment: this.form.comment,
                targetNodeId: this.form.targetNodeId,
              }
              await doRejectTaskToAny(data).then((res) => this.callbackFun(res))
            })
          }
        })
      },
      callbackFun(res) {
        if (res.success) {
          this.$baseMessage(res.msg, 'success')
          this.$emit('fetch-data')
          this.close()
        } else {
          this.$baseMessage(res.msg, 'error')
        }
      },
    },
  }
</script>
